package org.example.apply.xxljob;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.hash.Hash;
import com.bw.activity7.activiti.pojo.ActivitiVo;
import com.bw.activity7.activiti.pojo.UserTask;
import com.bw.activity7.activiti.service.ActivitiService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.example.apply.entity.Apply;
import org.example.apply.entity.UserDto;
import org.example.apply.feign.MsgFeign;
import org.example.apply.feign.UserFeign;
import org.example.apply.service.IApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class MyJob {

    @Autowired
    IApplyService applyService;

    @Autowired
    MsgFeign msgFeign;

    @Autowired
    UserFeign userFeign;

    @Autowired
    ActivitiService activitiService;

    /**
     * 财务对账
     */
    @XxlJob("task1")
    public void task1() {
        List<Apply> list = applyService.list();
        list.forEach(apply -> {
            if (apply.getPayStatus().equals(1)) {
                log.info("合同：{}，对账成功",apply.getTitle());
            }else {
                log.info("合同：{}，对账成功，转人工处理",apply.getTitle());
            }
        });
    }

    /**
     * 审批任务提醒
     */
    @XxlJob("task2")
    public void task2() {
        List<UserDto> list = userFeign.list();
        System.err.println("执行提醒审批"+DateUtil.now());
        for (UserDto userDto : list) {
            ActivitiVo vo = new ActivitiVo();
            vo.setUsername(userDto.getUsername());
            List<UserTask> userTasks = activitiService.queryUserTaskByUserName(vo);
            if (userTasks.size() > 0) {
                System.err.println(userTasks);
                msgFeign.send("87941125@qq.com","请登录系统，查看我的代办，完成合同审核");
            }
        }
    }

    /**
     * 模拟查询支付订单信息
     * @param applyId
     * @return
     */
    public Map<String, Object> payOrderInfo(String applyId) {
        HashMap<String, Object> orderMap = new HashMap<>();
        orderMap.put("applyId", applyId);
        orderMap.put("price", 100);
        return orderMap;
    }
}
